<?php
namespace app\api\controller\wdsxh;
use app\admin\model\wdsxh\Business as Model;
use app\admin\model\wdsxh\BusinessCat;
use app\admin\model\wdsxh\Member;
/**
 * 商圈接口
 */
class Business extends Base
{
    protected $noNeedLogin = ['cat','lists','detail','getMemberUnitList'];
    protected $noNeedRight = ['*'];

    public function cat(){
        $list=BusinessCat::where('status',1)->order('weigh desc')->field('id,name')->select();
        $this->success($list);
    }

    public function lists(){
        $page=$this->request->param('page',1);
        $limit=$this->request->param('limit',10);
        $cat=$this->request->param('cat',0);
        $keywords=$this->request->param('keywords',0);
        $map=array(
            'status'=>'1',
        );
        if(!empty($cat)){
            $map['cat_id']=$cat;
        }
        if(!empty($keywords)){
            $map['title']=['like',"%{$keywords}%"];
        }
        $total=Model::where($map)->count();
        $list=Model::where($map)
            ->order('weigh desc')
            ->page($page,$limit)
            ->select();
        if($list){
            foreach ($list as $row){
                $row['image']=wdsxh_full_url($row->image);
                $row->cat;
                $row->member;
                $row->company;
                if($row->company){
                    $row->company->company_logo=wdsxh_full_url($row->company->company_logo);
                }
                if($row->member){
                    $row->member->picture=wdsxh_full_url($row->member->picture);
                    $row->member->level;
                }

                if($row['mobile_hide'] == 1){
                    $row->member->mobile=wdsxh_hide_mobile($row->member->mobile);
                }

                $row['createtime']=date('Y-m-d H:i:s',$row['createtime']);
                $row->updatetime=date('Y-m-d H:i:s',$row['updatetime']);
            }
        }
        $data=array(
            'total'=>$total,
            'list'=>$list,
        );
        $this->success($data);
    }

    public function detail(){
        $id=$this->request->param('id');
        if(empty($id)){
            $this->error('id参数错误');
        }
        $data=Model::get($id);
        if(!$data){
            $this->error('信息不存在');
        }
        $data->read_num += 1;
        $data->save();
        $data['image']=wdsxh_full_url($data['image']);
        $data->cat;
        $data->member;
        $data->company;
        if($data->company){
            $data->company->company_logo=wdsxh_full_url($data->company->company_logo);
        }
        if($data->member){
            $data->member->picture=wdsxh_full_url($data->member->picture);
            $data->member->level;
        }
        if($data['mobile_hide'] == 1){
            $data->member->mobile=wdsxh_hide_mobile($data->member->mobile);
        }
//        if($data->member_unit_ids){
//            $data->member_units = model('app\admin\model\wdsxh\MemberUnit')->whereIn('id',$data->member_unit_ids)->column('company_name');
//        }

        $data['createtime']=date('Y-m-d H:i:s',$data['createtime']);
        $data['updatetime']=date('Y-m-d H:i:s',$data['updatetime']);

        $this->success($data);
    }


    /*
     * 发布商圈
     */
    public function add(){
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $this->request->filter('');
        $params=$this->request->param();
        $member=Member::where('uid',$this->auth->id)->find();
        if(!$member || $member['status'] != 1){
            $this->error('非会员不能发布商圈');
        }
        if($member['status'] != 1){
            $this->error('会员状态错误');
        }
        if(empty($params['cat_id'])){
            $this->error('cat_id参数错误');
        }
        if(empty($params['title'])){
            $this->error('title参数错误');
        }
        if(empty($params['image'])){
            $this->error('image参数错误');
        }
        $weigh=Model::where('status','1')->max('weigh');
        $params['uid']=$member['id'];
        $params['user_id']=$this->auth->id;
        $params['weigh']=$weigh;
        $params['status']='1';
        $params['mobile_hide']=intval($params['mobile_hide'])?1:0;
        $params['createtime']=time();
        $params['updatetime']=time();
//        $params['introduce']=$_POST['introduce'];
        $site=$this->configObj;
        if(empty($site['business_audit'])){
            if(!empty($params['member_unit_ids'])) {
                \app\common\model\BusinessModel::sendNewBusinessNotice($params);
            }
            $params['status']='1';
        }else{
            $params['status']='0';
        }
        $res=Model::create($params,true);
        if($res){
            $this->success('提交成功！');
        }else{
            $this->error('提交失败，请联系系统管理员');
        }
    }
    /*
     * 删除商圈
     */
    public function del(){
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $params=$this->request->param();
        $id=$params['id'];
        if(empty($id)){
            $this->error('请求参数错误！');
        }
        $member=Member::where('uid',$this->auth->id)->find();
        if(!$member){
            $this->error('会员信息不存在');
        }
        $res=Model::where('id',$id)->where('uid',$member->id)->find();
        if(!$res){
            $this->error('商圈信息不存在！');
        }
        $res->delete();
        $this->success('删除成功！');
    }
    /*
     * 编辑商圈
     */
    public function edit(){
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $params=$this->request->param();
        $id=$params['id'];
        if(empty($id)){
            $this->error('请求参数错误！');
        }
        $member=Member::where('uid',$this->auth->id)->find();
        if(!$member){
            $this->error('会员信息不存在');
        }
        $res=Model::where('id',$id)->where('uid',$member->id)->find();
        if(!$res){
            $this->error('商圈信息不存在！');
        }
        if($this->request->isPost()){
            $res['cat_id']=$params['cat_id'];
            $res['title']=$params['title'];
            $res['image']=$params['image'];
            $res['introduce']=$_POST['introduce'];
            $res['brief']=$_POST['brief'];
            $res['status']='0';
            $res['mobile_hide']=intval($params['mobile_hide'])?1:0;
            $res->save();
            $this->success('更新成功');
        }
        $this->success('success',$res);
    }
    /*
     * 发布历史
     */
    public function mylist(){
        $page=$this->request->param('page',1);
        $limit=$this->request->param('limit',10);
        $cat=$this->request->param('cat',0);

        $member=Member::where('uid',$this->auth->id)->find();
        if(!$member || $member['status'] != 1){
            $this->success();
        }

        $map=array(
            'uid'=>$member['id']
        );
        if(!empty($cat)){
            $map['cat_id']=$cat;
        }
        $total=Model::where($map)->count();
        $list=Model::where($map)
            ->order('weigh desc')
            ->page($page,$limit)
            ->select();
        if($list){
            foreach ($list as $row){
                $row['image']=wdsxh_full_url($row['image']);
                $row->cat;
                $row->member;
                $row->company;
                if($row->company){
                    $row->company->company_logo=wdsxh_full_url($row->company->company_logo);
                }
                $row['createtime']=date('Y-m-d H:i:s',$row['createtime']);
                $row['updatetime']=date('Y-m-d H:i:s',$row['updatetime']);
            }
        }
        $data=array(
            'total'=>$total,
            'list'=>$list,
        );
        $this->success($data);
    }

    /**
     * 获取会员单位列表
     */
    public function getMemberUnitList(\app\common\model\BusinessUnitModel $BusinessUnitModel){
        $data = $BusinessUnitModel->getList();
        $this->result($data['msg'], $data['data'], $data['code']);
    }

}